package net.app.zoneland.platform.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.app.zoneland.domain.SysMenu;
import net.app.zoneland.domain.SysUser;
import net.app.zoneland.vo.MenuTreeVo;
import net.app.zoneland.vo.MenuVO;
import org.apache.ibatis.annotations.*;
import java.util.List;

/**
 * @Description
 * @Author LENOVO
 * @CreateTime 2020/12/26 12:50
 * @Version: 1.0
 */
@Mapper
public interface SysMenuMapper extends BaseMapper<SysMenu> {

    /**
     * 查询目录和菜单
     * @return
     */
    @Select("<script>" +
            "   select id ,name ,parent_id,path,component,perms,icon,sort,visible,status from sys_menu " +
            "   where status=1 and type in (0,1)" +
            "   order by sort asc" +
            "</script>")
    @Results({
            @Result(id=true, column="id", property="id"),
            // 一对多关联查询拥有菜单访问权限的角色ID集合
            @Result(property = "roles",column="id",many = @Many(select="net.app.zoneland.platform.mapper.SysRoleMenuMapper.listByMenuId"))
    })
    List<SysMenu> listForRouter();

    /*根据用户账号查询菜单*/
    List<MenuVO> queryMenuByUsername(String username);

    /*根据父类ID查询子类*/
    List<MenuVO> findTreeByPid(String id);

    /**
     * 查询菜单信息分页
     * @param page
     * @param menu
     * @return
     */
    @Select("<script> " +
            "   select u.* from sys_menu u" +
            "   where 1=1 " +
            " <if test ='menu.name!=null and menu.name.trim() neq \"\"'>" +
            "       and u.name like concat('%',#{menu.name},'%')" +
            " </if>" +
            " <if test ='menu.type!=null'>" +
            "       and u.type = #{menu.type}" +
            " </if>" +
            "</script>")
    IPage<SysMenu> list(Page<SysMenu> page, SysMenu menu);

    /**
     *
     * @param menu
     * @return
     */
    @Select("<script> " +
            "   select u.* from sys_menu u join sys_role_menu rm on rm.menu_id=u.id " +
            "   where 1=1 " +
            " <if test ='menu.name!=null and menu.name.trim() neq \"\"'>" +
            "       and u.name like concat('%',#{menu.name},'%')" +
            " </if>" +
            " <if test ='menu.type!=null'>" +
            "       and u.type = #{menu.type}" +
            " </if>" +
            " <if test ='menu.roleIds!=null and menu.roleIds.trim() neq \"\"'>" +
            "       and rm.role_id in (#{menu.roleIds})" +
            " </if>" +
            " ORDER BY sort "+
            "</script>")
    List<SysMenu> listByRoleId(@Param("menu") SysMenu menu);

    @Select("<script> " +
            "   select u.id,u.parent_id parentId,u.name menuName,u.perms,u.path menuUrl,u.type isMenu," +
            " u.sort, u.icon menuIcon,u.gmt_create createTime from sys_menu u " +
            "   where 1=1 " +
            " <if test ='menu.name!=null and menu.name.trim() neq \"\"'>" +
            "       and u.name like concat('%',#{menu.name},'%')" +
            " </if>" +
            " <if test ='menu.type!=null'>" +
            "       and u.type = #{menu.type}" +
            " </if>" +
            "</script>")
    List<MenuTreeVo> getMenus(@Param("menu") SysMenu menu);

    void deleteByBatch(@Param("ids")List<Integer> ids);
}
